package com.bins.dentist.clinic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bins.dentist.clinic.constants.CommonConstants;
import com.bins.dentist.clinic.entity.ClinicEntity;
import com.bins.dentist.clinic.mapper.ClinicMapper;
import com.bins.dentist.clinic.model.dto.ClinicDto;
import com.bins.dentist.clinic.model.dto.ClinicPageDto;
import com.bins.dentist.clinic.model.vo.ClinicVo;
import com.bins.dentist.clinic.model.vo.SimpleSelectVo;
import com.bins.dentist.clinic.service.ClinicService;
import com.bins.dentist.clinic.utils.GenerateUtil;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 诊所表 服务实现类
 * </p>
 *
 * @author bins
 * @since 2023-04-23 13:22:36
 */
@Service
public class ClinicServiceImpl extends ServiceImpl<ClinicMapper, ClinicEntity> implements ClinicService {

    @Autowired
    private ClinicMapper clinicMapper;

    @Override
    public IPage<ClinicVo> getPage(ClinicPageDto pageDto) {
        LambdaQueryWrapper<ClinicEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(!ObjectUtils.isEmpty(pageDto.getClinicCode()), ClinicEntity::getClinicCode, pageDto.getClinicCode())
                .like(!ObjectUtils.isEmpty(pageDto.getClinicName()), ClinicEntity::getClinicName, pageDto.getClinicName())
                .eq(!ObjectUtils.isEmpty(pageDto.getClinicMobile()), ClinicEntity::getClinicMobile, pageDto.getClinicMobile())
                .eq(ClinicEntity::getIsDel, CommonConstants.DELETED_NO);
        IPage page = clinicMapper.selectPage(new Page(pageDto.getPageNum(), pageDto.getPageSize()), queryWrapper);
        List<ClinicEntity> pageList = page.getRecords();
        if (CollectionUtils.isEmpty(pageList)) {
            return page;
        }
        List<ClinicVo> dateList = Lists.newArrayListWithCapacity(pageList.size());
        for (ClinicEntity item : pageList) {
            ClinicVo itemVo = new ClinicVo();
            BeanUtils.copyProperties(item, itemVo);
            dateList.add(itemVo);
        }
        page.setRecords(dateList);
        return page;
    }

    @Override
    public ClinicVo getDetail(ClinicDto dto) {
        ClinicEntity item = this.baseMapper.selectById(dto.getId());
        ClinicVo resultVo = new ClinicVo();
        BeanUtils.copyProperties(item, resultVo);
        return resultVo;
    }

    @Override
    public ClinicVo saveItem(ClinicDto dto) {
        ClinicEntity item = new ClinicEntity();
        BeanUtils.copyProperties(dto, item);
        if (ObjectUtils.isEmpty(dto.getClinicCode())) {
            item.setClinicCode(GenerateUtil.generateClinicCode("C"));
        }
        item.setUpdateBy(CommonConstants.DEFAULT_USER_ID);
        item.setUpdateName(CommonConstants.DEFAULT_USER_NAME);
        item.setUpdateTime(LocalDateTime.now());
        if (ObjectUtils.isEmpty(dto.getId())) {
            item.setIsDel(CommonConstants.DELETED_NO);
            item.setCreateBy(CommonConstants.DEFAULT_USER_ID);
            item.setCreateName(CommonConstants.DEFAULT_USER_NAME);
            item.setCreateTime(LocalDateTime.now());
            this.baseMapper.insert(item);
        } else {
            this.getBaseMapper().updateById(item);
        }
        ClinicVo resultVo = new ClinicVo();
        resultVo.setId(item.getId());
        return resultVo;
    }

    @Override
    public Integer deleteItem(ClinicDto dto) {
        ClinicEntity existItem = this.getBaseMapper().selectById(dto.getId());
        ClinicEntity item = new ClinicEntity();
        item.setId(dto.getId());
        item.setIsDel(CommonConstants.DELETED_YES);
        return this.getBaseMapper().updateById(item);
    }

    @Override
    public List<SimpleSelectVo> selectList(ClinicDto dto) {
        LambdaQueryWrapper<ClinicEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(ClinicEntity::getIsDel, CommonConstants.DELETED_NO);
        List<ClinicEntity> queryList = this.getBaseMapper().selectList(queryWrapper);
        if (CollectionUtils.isEmpty(queryList)) {
            return Collections.emptyList();
        }
        List<SimpleSelectVo> resultList = Lists.newArrayListWithCapacity(queryList.size());
        SimpleSelectVo simpleSelectVo;
        for (ClinicEntity item : queryList) {
            simpleSelectVo = new SimpleSelectVo();
            simpleSelectVo.setId(item.getId());
            simpleSelectVo.setName(item.getClinicName());
            resultList.add(simpleSelectVo);
        }
        return resultList;
    }
}
